
import Question.Problem;
/**
 * Description:机考图形界面
 */
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.Enumeration;

public class testWindow implements ActionListener{

    Timer timer = new Timer(1000,this);//计时器
    long tReaming = 20;//每一题的剩余时间
    long time = 0;//总计用时

    JFrame frame = new JFrame();//容器
    JPanel north = new JPanel();//上侧模块 - 用于表示总用时
    JLabel totalTime = new JLabel("已经用时：");
    JLabel toTime = new JLabel(time+"");

    JPanel east = new JPanel();//右侧模块 - 剩余时间
    JLabel TimeRemaining = new JLabel("剩余时间：");
    JLabel timeRemaining = new JLabel(tReaming+"");

    Box center = Box.createVerticalBox();//中心模块 - 问题
    JLabel question = new JLabel();//问题

    //选项模块
    Box choice1 = Box.createVerticalBox();//多选
    Box choice0 = Box.createVerticalBox();//单选
    Box choice2 = Box.createVerticalBox();//判断
    ButtonGroup group0 = new ButtonGroup();//创建一个单选题的按钮组
    ButtonGroup group2 = new ButtonGroup();//创建一个判断题的按钮组
    //复选框按钮
    JCheckBox a1 = new JCheckBox();
    JCheckBox b1 = new JCheckBox();
    JCheckBox c1 = new JCheckBox();
    JCheckBox d1 = new JCheckBox();
    //单选框按钮组
    JRadioButton a0 = new JRadioButton();
    JRadioButton b0 = new JRadioButton();
    JRadioButton c0 = new JRadioButton();
    JRadioButton d0 = new JRadioButton();
    //判断题按钮组
    JRadioButton r = new JRadioButton();
    JRadioButton w = new JRadioButton();

    JButton submit = new JButton("确定");//提交按钮
    JLabel right_aswer = new JLabel();//正确答案标签

    RandomAcquirement acquirement = new RandomAcquirement();//抽题系统
    Problem[] problems = new Problem[3];//题库
    String[] type = new String[]{"单选题","多选题","判断题"};//题型
    String[] C;//存储选项内容
    int t;//题型
    int n;//题库中的对应题序号
    int number = 0;//已出题目数量
    int[] type_number = acquirement.getTypeNum();//各个类型已经出题数量

    JPanel south = new JPanel();//最下面部分 - 做题情况
    JPanel title = new JPanel();//出题情况
    JLabel single = new JLabel();//单选题情况
    JLabel multiple = new JLabel();//多选题情况
    JLabel judge = new JLabel();//判断题情况

    String[] my_aswer = new String[15];//用户答案
    String[] cr_aswer = new String[15];//正确答案
    int[] score = new int[3];//记录各题型正确情况
    int totalScore = 0;//记录总分
    JLabel l_totalScore = new JLabel();//总分
    JPanel Score = new JPanel();//题目正确情况
    JLabel sc_single = new JLabel();//单选题正确情况
    JLabel sc_multiple = new JLabel();//多选题正确情况
    JLabel sc_judge = new JLabel();//判断题正确情况



    //main方法
    public static void main(String[] args) {
        Font font = new Font("楷体",0,26);//创建字体对象
        testWindow.InitGlobalFont(font);//将默认字体设置为全局字体
        testWindow test = new testWindow();//创建一个java机考图形界面
    }

    public testWindow(){//构造方法

        //设置上方布局 - 总用时
        north.setLayout(new GridLayout(2,0,0,0));
        north.add(totalTime);
        north.add(toTime);

        //设置右侧布局 - 剩余时间
        east.setLayout(new GridLayout(2,1,0,0));
        east.add(TimeRemaining);
        timeRemaining.setFont(new Font("楷体",0,40));//设置剩余时间字体
        east.add(timeRemaining);

        try {//准备题目
            problems[0] = new Problem("E:\\方秋慧\\JAVA程序设计\\单选.txt",4);
            problems[1] = new Problem("E:\\方秋慧\\JAVA程序设计\\多选.txt",4);
            problems[2] = new Problem("E:\\方秋慧\\JAVA程序设计\\判断.txt",2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        //出题
        t = acquirement.setType();//抽取题型
        n = acquirement.setNumber();//抽取对应题号
        question.setText("["+type[t]+"] "+(number+1)+"、"+problems[t].getQuestion(n).getQ());//设置问题
        center.add(question);//添加组件

        //多选题的选项
        choice1.add(a1);
        choice1.add(b1);
        choice1.add(c1);
        choice1.add(d1);
        //创建一个按钮组 - 同时只能选其中一个选项
        ButtonGroup group1 = new ButtonGroup();//单选题的按钮组
        ButtonGroup group2 = new ButtonGroup();//判断题的按钮组
        //单选题的选项
        choice0.add(a0);
        choice0.add(b0);
        choice0.add(c0);
        choice0.add(d0);
        //添加到按钮组中
        group1.add(a0);
        group1.add(b0);
        group1.add(c0);
        group1.add(d0);

        //判断题的选项
        group2.add(r);
        group2.add(w);
        //添加到按钮组中
        choice2.add(r);
        choice2.add(w);

        choice0.setVisible(false);//暂时设置为不可见
        center.add(choice0);
        choice1.setVisible(false);
        center.add(choice1);
        choice2.setVisible(false);
        center.add(choice2);

        if (t==1){//多选列表
            newMul();//设置多选选项内容
        }else if (t==0){
            newSingle();//设置单选选项内容
        }else {
            newjdge();//设置判断选项内容
            choice2.setVisible(true);
        }
        submit.addActionListener(this);
        center.add(submit);//添加确认按钮
        center.add(right_aswer);//添加答案组件

        //初始化做题数目标签
        single.setText("单选题：已做"+type_number[0]+"道");
        multiple.setText("多选题：已做"+type_number[1]+"道");
        judge.setText("判断题：已做"+type_number[2]+"道");
        //添加做题情况的标签
        title.setLayout(new GridLayout(1,1,1,0));
        title.add(single);
        title.add(multiple);
        title.add(judge);
        //初始化答对的数目的标签
        sc_single.setText("单选题：正确"+score[0]+"道");//每题一分
        sc_multiple.setText("多选题：正确"+score[1]*2+"道");//每题两分
        sc_judge.setText("判断题：正确"+score[2]+"道");//每题一分
        l_totalScore.setText("总分："+totalScore);//初始化总分
        //添加到窗格中
        Score.setLayout(new GridLayout(1,1,0,0));
        Score.add(sc_single);
        Score.add(sc_multiple);
        Score.add(sc_judge);

        south.setLayout(new GridLayout(2,1,0,0));
        south.add(title);
        south.add(Score);

        //将前面布局的组件添加到窗格中
        frame.setLayout(new BorderLayout());
        frame.add(north,BorderLayout.NORTH);
        frame.add(east,BorderLayout.EAST);
        frame.add(center,BorderLayout.CENTER);
        frame.add(south,BorderLayout.SOUTH);

        timer.start();//开始计时

        frame.setTitle("单机版java简易机考程序");//设置窗口标题
        frame.validate();//刷新窗口
        frame.setSize(1200,700);//设置窗口大小
        frame.setLocationRelativeTo(null);//设置窗口默认居中
        frame.setVisible(true);//设置窗口可见
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public void newSingle(){//更新单选题的选项
        String[] newChoice = problems[t].getQuestion(n).getChoice();//获取新选项内容
        a0.setText(newChoice[0]);//设置选项
        b0.setText(newChoice[1]);
        c0.setText(newChoice[2]);
        d0.setText(newChoice[3]);
        group0.clearSelection();
        choice0.setVisible(true);//设置选项模块可见
        frame.validate();
    }

    public void newMul(){//更新多选题的选项
        String[] newChoice = problems[t].getQuestion(n).getChoice();//获取新选项内容
        a1.setText(newChoice[0]);//设置选项
        b1.setText(newChoice[1]);
        c1.setText(newChoice[2]);
        d1.setText(newChoice[3]);
        choice1.setVisible(true);//设置选项模块可见
        frame.validate();
    }

    public void newjdge(){//更新判断题的选项
        String[] newChoice = problems[t].getQuestion(n).getChoice();//获取新选项内容
        r.setText(newChoice[0]);//设置选项
        w.setText(newChoice[1]);
        group2.clearSelection();
        choice2.setVisible(true);//设置选项模块可见
        frame.validate();
    }

    public void showNumber(){//显示答题数量
        cr_aswer[number] = problems[t].getQuestion(n).getAnswer();//正确答案
        right_aswer.setText("上一题正确答案为："+cr_aswer[number]);//展示正确答案
        type_number = acquirement.getTypeNum();//获取各类型题目答题数
        single.setText("单选题：已做"+type_number[0]+"道");
        multiple.setText("多选题：已做"+type_number[1]+"道");
        judge.setText("判断题：已做"+type_number[2]+"道");
        frame.validate();
    }

    public void showScore(){//计分
        my_aswer[number] = "";//答案
        if(t==1){//多选
            if (a1.isSelected()){
                my_aswer[number]+="A";
            }
            if (b1.isSelected()){
                my_aswer[number]+="B";
            }
            if (c1.isSelected()){
                my_aswer[number] = "C";
            }
            if (d1.isSelected()){
                my_aswer[number] = "D";
            }
        }else if (t==0){//单选
            if (a0.isSelected()){
                my_aswer[number] = "A";
            }
            if (b0.isSelected()){
                my_aswer[number] = "B";
            }
            if (c0.isSelected()){
                my_aswer[number] = "C";
            }
            if (d0.isSelected()){
                my_aswer[number] = "D";
            }
        }else {//判断
            if (r.isSelected()){
                my_aswer[number] = "A";
            }
            if (w.isSelected()){
                my_aswer[number] = "B";
            }
        }
        if (my_aswer[number].equals(cr_aswer[number])){
            if (t==0){//单选题正确
                score[0]++;
                totalScore++;
            }else if (t==1){//多选题正确
                score[1]++;
                totalScore+=2;
            }else {//判断题正确
                score[2]++;
                totalScore++;
            }
        }
        sc_single.setText("单选题：正确"+score[0]+"道");//每题一分
        sc_multiple.setText("多选题：正确"+score[1]+"道");//每题两分
        sc_judge.setText("判断题：正确"+score[2]+"道");//每题一分
        l_totalScore.setText("总分："+totalScore);//总分
    }

    public void refresh(){
        showNumber();//展示已经做题数
        showScore();//计算并更新答对数目和分数
        //将选择模块全部设置为不可见，避免后续冲突
        choice0.setVisible(false);
        choice1.setVisible(false);
        choice2.setVisible(false);

        //抽取新题目
        number++;
        t = acquirement.setType();
        n = acquirement.setNumber();
        //生成新问题
        question.setText("["+type[t]+"] "+(number+1)+"、"+problems[t].getQuestion(n).getQ());
        //设置新的选项
        if (t==0){
            newSingle();
        }else if (t==1){
            newMul();
        }else {
            newjdge();
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (number==14){//答题结束
            showNumber();//展示已经做题数
            showScore();//计算并更新答对数目和分数
            //弹出结束对话框
            JOptionPane.showMessageDialog(null,"得分为："+totalScore+"\n恭喜你！",
                    "考试结束",JOptionPane.INFORMATION_MESSAGE);
            System.exit(0);//退出程序
        }
        if (e.getSource()==submit){//点击了提交
            refresh();//更新界面，获得新题
            tReaming = 20;
            timer.restart();//重新开始倒计时
            TimeRemaining.setText("剩余时间：");
        }
        if (e.getSource()==timer){
            tReaming--;//倒计时剩余时间
            time++;//考试总用时
            int min = 0;
            if(time>60){
                min = (int) (time/60);
            }
            toTime.setText(min+"分"+(time%60)+"秒");
            timeRemaining.setText(tReaming+"s");
            if (tReaming==0){//当一题的答题时间到，则自动提交，开始下一题
                refresh();
                TimeRemaining.setText("时间到！");
                tReaming = 20;
                timer.restart();//重新开始倒计时
                TimeRemaining.setText("剩余时间：");
            }
        }
        frame.validate();
    }

    //修改swing全局字体 - 在main方法中生成其他GUI图形界面之前调用，可以将字体设置为我们传参的字体
    public static void InitGlobalFont(Font f){
        FontUIResource fontRes = new FontUIResource(f);
        for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof FontUIResource) {
                UIManager.put(key, fontRes);
            }
        }
    }
}
